package com.sage.logbook.config;

import java.io.File;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sage.logbook.config.bean.ServerConfigBean;
import com.sage.logbook.util.BeanUtils;

public class ServerConfig {
    private static final Logger LOGGER = LoggerFactory.getLogger(ServerConfig.class);

    private static ServerConfigBean config = new ServerConfigBean();

    private static final File CONFIG_FILE = new File("./config/server.xml");

    private ServerConfig(){
    }

    public static void load() {
        try {
            ServerConfigBean bean = BeanUtils.readObject(CONFIG_FILE, ServerConfigBean.class);
            if (bean != null){
                config = bean;
            }
        } catch (Exception e) {
            LOGGER.warn("アプリケーション設定を読み込みますに失敗しました", e);
        }
    }

    public static void store() throws IOException {
        BeanUtils.writeObject(CONFIG_FILE, config);
    }

    public static boolean isAllowOnlyFromLocalhost() {
        return config.isAllowOnlyFromLocalhost();
    }

    public static int getListenPort() {
        return config.getListenPort();
    }

    public static boolean useProxy() {
        return config.isUseProxy();
    }

    public static String getProxyHost() {
        return config.getProxyHost();
    }

    public static int getProxyPort() {
        return config.getProxyPort();
    }
}
